import pandas as pd
import requests
import json

from tools.wrappers import deco_retry


@deco_retry(retry=50, retry_sleep=15)
def get_btc126_usdtcny_premium():
    header = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'}
    url = 'https://history.btc126.com/usdt/api.php'
    requests_session = requests.Session()
    requests_session.trust_env = False  # 不走VPN代理
    res = requests_session.get(url, headers=header, timeout=15)
    df = pd.DataFrame(json.loads(res.content)['data'])
    df['premium'] = (df['usdt'].astype(float) - df['usd'].astype(float)) / df['usd'].astype(float)
    return df


if __name__ == '__main__':
    get_btc126_usdtcny_premium()
